<?php
C_base::yn_file('base'.DS.'htmlbase.php');
class im extends htmlbase {
	
	function __construct(){
		parent::__construct();
	}
	function index(){
		
		$this->_is_html = 1;
		@extract($this->_post());
		//进行到这里证明服务器是在正常通讯并安检过后是制定的客户端发送过来的请求的数据处理了。
		//在处理请求的时候记得安全过滤(慎重注意)
		//任何操作都是用apitype传递根参数。区别所对应的操作。如果无表示非法操作
		//@fanhui是返回的当前客户端的数据信息
		$apitype =@$apitype ? @$apitype : '';
		if ($apitype == 'denglu'){
			echo $this->fanhuijiami(self::denglu($this->_post()));
		}elseif ($apitype == 'xiu_user'){
			echo $this->fanhuijiami(self::xiu_user($this->_post()));
		}elseif ($apitype == 'get_user'){
			echo $this->fanhuijiami(self::get_user($this->_post()));
		}elseif ($apitype == 'room_c'){
			echo $this->fanhuijiami(self::room_c($this->_post()));
		}elseif ($apitype == 'room_ss'){
			echo $this->fanhuijiami(self::room_ss($this->_post()));
		}elseif ($apitype == 'room_del'){
			echo $this->fanhuijiami(self::room_del($this->_post()));
		}elseif ($apitype == 'liaot_c'){
			echo $this->fanhuijiami(self::liaot_c($this->_post()));
		}elseif ($apitype == 'liaot_qc'){
			echo $this->fanhuijiami(self::liaot_qc($this->_post()));
		}else{
			echo '非法操作';
		}
	}
	
	/*
	 * 安全加密封装
	*/
	function fanhuijiami($content){
		return json_encode(serialize($content));
	}
	
/****************************API***********************************/
	/**
	 * 登陆
	 * name 账号
	 * pass 密码
	 */
	function denglu($post){
		extract($post);
		$cha = C_base::M('admin.recruit')->user_s(array('phone'=>$name,'pass'=>md5($pass)));
		if(!@$cha){
			$cha = C_base::M('admin.recruit')->user_s(array('email'=>$name,'pass'=>md5($pass)));
		}
		if(@$cha){
			if($cha['dongjie'] == 1){
				return '-1';//用户被冻结
			}else{
				return $cha['id'];
			}
		}else{
			return '-2';  //用户名或密码错误
		}
	}
	/**
	 * 修改用户
	 * id 用户id
	 * socket_id
	 */
	function xiu_user($post){
		extract($post);
		$xiu = C_base::M('admin.recruit')->user_u(array('id'=>$id),array('socket_id'=>$socket_id));
		if($xiu){
			return '1';	
		}else{
			return '-1';
		}	
	}
	/**
	 * 获取用户
	 * socket_id/id
	 */
	function get_user($post){
		extract($post);
		if(@$id){
			$cha = C_base::M('admin.recruit')->user_s(array('id'=>$id));
		}elseif(@$socket_id){
			$cha = C_base::M('admin.recruit')->user_s(array('socket_id'=>$socket_id));
		}else{
			return '-1';
		}
		return $cha;
	}

	/**
	 * 写入房间
	 * uid 成员id
	 * room 房间id
	 */
	function room_c($post){
		extract($post);
		$w =array('uid'=>$uid,'room'=>$room);
		$cz = C_base::M('admin.recruit')->room_s($w);
		if(@$cz){
			return 1;
		}
		$xr = C_base::M('admin.recruit')->room_c($w);
		if($xr){
			return 1;
		}else{
			return -1;
		}
	}
	
	/**
	 * 查询房间成员
	 * room 房间id
	 */
	function room_ss($post){
		extract($post);
		$ss = C_base::M('admin.recruit')->room_ss(array('room'=>$room));
		if($ss){
			return $ss;
		}else{
			return '-1';
		}
	}
	/**
	 * 解散临时房间
	 * room 房间id
	 */
	function room_del($post){
		extract($post);
		if(@$room){
			C_base::M('admin.recruit')->room_del(array('room'=>$room));
		}
	}
	
	/**
	 *单聊-写入聊天记录
	 */
	function liaot_c($post){
		extract($post);
		if(@$uid && @$did){
			$fq=array('uid'=>$uid,'did'=>$did,'content'=>$content,'time'=>time(),'type'=>0);
			$fqz=C_base::M('admin.recruit')->liaot_c($uid,$fq);//发起者
			$js=array('uid'=>$did,'did'=>$uid,'content'=>$content,'time'=>time(),'type'=>1);
			$jsz=C_base::M('admin.recruit')->liaot_c($did,$js);//接收者
			if($fqz && $jsz){
				return '1';
			}
		}
	}

	/**
	 *临时群聊-写入聊天记录
	 */
	function liaot_qc($post){
		extract($post);
		if(@$uid && @$room){
			$fq=array('uid'=>$uid,'room'=>$room,'content'=>$content,'time'=>time());
			$fqz=C_base::M('admin.recruit')->liaot_qc($room,$fq);//发起者
			if($fqz){
				return '1';
			}
		}
	}
	
}
?>